package com.jochen.core.ui.realize

import com.badlogic.gdx.graphics.*
import com.jochen.core.ui.onClick
import com.artemis.World
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.math.Interpolation
import com.badlogic.gdx.scenes.scene2d.actions.Actions
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.jochen.core.assets.AtlasAsset
import com.jochen.core.assets.SkinAsset
import com.jochen.core.screen.GameScreen
import com.jochen.core.world.GdxGame
import com.jochen.core.system.*

class BuilingSelectUi(
    private val game: GdxGame,
    private val gameScreen: GameScreen
) : Table() {

    private var curEntity: Int = -1
    private val animationDuration: Float = 0.3f
    
    init {
        setFillParent(true)
        isVisible = false
        color.a = 0f 
        y = gameScreen.screenHeight.toFloat() 
        initUi()
    }

    lateinit var objName: Label
    lateinit var describeLabel: Label
    
    private fun initUi() {
        val sk = game.assetService.get(SkinAsset.DEFAULT) 
        
        objName = Label("单位名字", sk).apply{
            setFontScale(0.5f)
            color = Color(1f,0f,0f,1f)
        }
        describeLabel = Label("有个鸡毛",sk).apply{
            setVisible(false)
            setFontScale(0.5f)
            color = Color(0f,1f,0f,1f)
        }
        
        val menuAtlas = game.assetService.get(AtlasAsset.MENU)
        val deleteButton = ImageButton(UiStyle.getImageButtonStyle(menuAtlas, "057"))//移除
        val quickConstructionButton = ImageButton(UiStyle.getImageButtonStyle(menuAtlas, "131"))//链接
        val quickPlatform = ImageButton(UiStyle.getImageButtonStyle(menuAtlas, "130"))//新平台
        val questionMark = ImageButton(UiStyle.getImageButtonStyle(menuAtlas, "128"))//详细
        
        questionMark.onClick{
            if(!describeLabel.isVisible){
                startAutoFadeCycle()
            }
        }
        
        pad(5f).top().left()
        defaults().padRight(10f).padBottom(5f).uniform().size(50f)
        
        add(objName).row()
        add(quickConstructionButton)
        add(quickPlatform)
        add(questionMark)
        add(deleteButton)
        add(describeLabel).colspan(5)
    }
    
    fun setEntity(entity: Int) {
      
        clearActions()
        
        if (entity == -1) {
            this.curEntity = -1
            addAction(Actions.sequence(
                Actions.fadeOut(animationDuration),
                Actions.visible(false)
            ))
            if(describeLabel.isVisible){
                stopAutoFadeCycle()
            }
        } else {
            this.curEntity = entity
            
            isVisible = true
            color.a = 0f
            y = gameScreen.screenHeight.toFloat()
            
            addAction(Actions.parallel(
                Actions.fadeIn(animationDuration),
                Actions.moveTo(x, 0f, animationDuration , Interpolation.smooth)
            ))
            updateUi(entity)
        }
    }
    
    fun updateUi(entityId: Int){
        val building = Mapper.buildings.get(entityId)
        building.buildingData?.let{ data ->
            objName.setText("平台 : ${data.buildingName}    等级 ${building.level}")
            describeLabel.setText("描述 : ${data.describe}")
        }
        
    }
    
    fun startAutoFadeCycle() {
        describeLabel.clearActions()
        describeLabel.isVisible = true
        describeLabel.color.a = 0f
        
        describeLabel.addAction(Actions.forever(
            Actions.sequence(
                Actions.fadeIn(0.5f), 
                Actions.delay(1f),      
                Actions.fadeOut(2f),  
                Actions.visible(false)     
            )))
    }
    
    fun stopAutoFadeCycle() {
        describeLabel.clearActions()
        describeLabel.color.a = 1f 
        describeLabel.isVisible = false
    }
}